//
//  TBWithdrawBackView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/2/28.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

protocol TBWithdrawBackViewDelegate:NSObjectProtocol {
    func TBWithdrawBackViewAction()
}

class TBWithdrawBackView: UIView, UITextFieldDelegate {
    var delegate:TBWithdrawBackViewDelegate?
    lazy var nameField:UITextField = {
        let text = UITextField.init(frame: FRAME_CGFloat(122.5, 64.5, SCREEN_WIDTH - 125, 16.5))
        text.font = FONT(12)
        text.placeholder = "请输入您的姓名"
        return text
    }()

    lazy var phoneField:UITextField = {
        let text = UITextField.init(frame: FRAME_CGFloat(122.5, 113.5, SCREEN_WIDTH - 125, 16.5))
        text.font = FONT(12)
        text.placeholder = "请输入您的手机号"
        return text
    }()
    
    lazy var codeField:UITextField = {
        let text = UITextField.init(frame: FRAME_CGFloat(122.5, 162.5, SCREEN_WIDTH - 125, 16.5))
        text.font = FONT(12)
        text.placeholder = "请输入验证码"
        return text
    }()
    
    lazy var bindingBtn:UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(70, 217.5, SCREEN_WIDTH - 140, 40)
//        btn.setBackgroundImage(IMAGE(name: "Rectangle"), for: .normal)
        btn.layer.cornerRadius = 18
        btn.backgroundColor = RGB(87, 154, 255)
        btn.setTitle("提交", for: .normal)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        btn.addTarget(self, action: #selector(bindingBtnAction), for: .touchUpInside)
        return btn
    }()
    
    lazy var validateCodeBtn: TTValidButton = {
        let btn = TTValidButton.init(type: .custom)
        btn.frame = FRAME_CGFloat(SCREEN_WIDTH - 140, 207 - 48, 100                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   , 28)
        btn.addTarget(self, action: #selector(validateCodeBtnAction), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        self.isUserInteractionEnabled = true
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//  MARK: - ###### UI ######
extension TBWithdrawBackView {
    func setUI() {
        let backImageView = UIImageView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP))
        backImageView.backgroundColor = RGB_Same(0, 0.75)
        self.addSubview(backImageView)
        backImageView.isUserInteractionEnabled = true
//        backImageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(hiddenAction)))
        
        let whiteView = UIImageView.init(frame: FRAME_CGFloat(0, SCREEN_HEIGHT - OFFSET_TOP - 307, SCREEN_WIDTH, 318))
        whiteView.layer.cornerRadius = 10
        whiteView.backgroundColor = RGB(255, 249, 249)
        backImageView.addSubview(whiteView)
        whiteView.isUserInteractionEnabled = true
        
        let topLabel = UILabel.init(frame: FRAME_CGFloat(0, 21, SCREEN_WIDTH, 20))
        topLabel.font = FONT(14)
        topLabel.textColor = RGB_Same(0)
        topLabel.text = "为了您的财产安全，请填写以下信息"
        topLabel.textAlignment = .center
        whiteView.addSubview(topLabel)
        
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(13, 13, 15, 15)
        button.addTarget(self, action: #selector(hiddenAction), for: .touchUpInside)
        button.setImage(IMAGE(name: "payOrderClose"), for: .normal)
        whiteView.addSubview(button)
        
        let nameLabel = UILabel.init(frame: FRAME_CGFloat(42, 63.5, 40, 20))
        nameLabel.font = FONT(14)
        nameLabel.textColor = RGB_Same(53)
        nameLabel.text = "姓   名"
        whiteView.addSubview(nameLabel)
        whiteView.addSubview(self.nameField)
        
        let nameLineView = UIView.init(frame: FRAME_CGFloat(27.5, 91.2, SCREEN_WIDTH - 55, 1))
        nameLineView.backgroundColor = RGB_Same(240, 1)
        whiteView.addSubview(nameLineView)
        
        let phoneLabel = UILabel.init(frame: FRAME_CGFloat(42, 112.5, 60, 20))
        phoneLabel.font = FONT(14)
        phoneLabel.textColor = RGB_Same(53)
        phoneLabel.text = "手机号"
        whiteView.addSubview(phoneLabel)
        whiteView.addSubview(self.phoneField)
        
        let phoneLineView = UIView.init(frame: FRAME_CGFloat(27.5, 140.2, SCREEN_WIDTH - 55, 1))
        phoneLineView.backgroundColor = RGB_Same(240, 1)
        whiteView.addSubview(phoneLineView)
        
        let codeLabel = UILabel.init(frame: FRAME_CGFloat(42, 161.5, 60, 20))
        codeLabel.font = FONT(14)
        codeLabel.textColor = RGB_Same(53)
        codeLabel.text = "验证码"
        whiteView.addSubview(codeLabel)
        whiteView.addSubview(self.codeField)
        
        let codeLineView = UIView.init(frame: FRAME_CGFloat(27.5, 189.2, SCREEN_WIDTH - 55, 1))
        codeLineView.backgroundColor = RGB_Same(240, 1)
        whiteView.addSubview(codeLineView)
        
        whiteView.addSubview(validateCodeBtn)
        whiteView.addSubview(bindingBtn)
        validateCodeBtn.setTitle("获取验证码", for: .normal)
        validateCodeBtn.setTitleColor(BlackBackColor, for: .normal)
        
    }
}

//  MARK: - ###### 事件 ######
extension TBWithdrawBackView {
    @objc func hiddenAction() {
        self.isHidden = true
    }
    
    @objc func bindingBtnAction() {
        MineRequestModel.CheckVerCode(parameters: ["mobile":self.phoneField.text!, "name":self.nameField.text!,"verCode":self.codeField.text!,"userId":String.getUserId()]) { (suc, mes) in
            if suc {
                self.delegate?.TBWithdrawBackViewAction()
                self.isHidden = true
            } else {
                TTAlertView.showMessage(message: mes)
            }
        }
    }
}

//  MARK: - ###### 私有方法 ######
extension TBWithdrawBackView {
    @objc func validateCodeBtnAction() {
        if !TTValidator.isTelNumber(num: phoneField.text!) {
            TTAlertView.showMessage(message: "请输入正确的手机号码！")
            return ;
        }
        
        weak var weakSelf = self
        weakSelf?.validateCodeBtn.startTime(60)
        
        MineRequestModel.LoadVerCode(jointArr: [self.phoneField.text!,String.getUserId()], parameters:["type":"login"]) { (suc, mes) in
            TTAlertView.showMessage(message: mes)
        }
    }
    
    fileprivate func setLoginBtnStatue(isSelected: Bool) {
        if isSelected {
            self.bindingBtn.setTitleColor(WhiteBackColor, for: UIControl.State.normal)
            self.bindingBtn.isUserInteractionEnabled = true
        } else {
            self.bindingBtn.setTitleColor(WhiteBackColor, for: UIControl.State.normal)
            self.bindingBtn.isUserInteractionEnabled = false
        }
    }
    
    @objc fileprivate func textFieldDidChange() {
        
        self.phoneField.text = limitInput(digit: 11, input: phoneField.text!)
        self.codeField.text = limitInput(digit: 6, input: codeField.text!)
        
        if checkInput() {
            setLoginBtnStatue(isSelected: true)
        } else {
            setLoginBtnStatue(isSelected: false)
        }
    }
    
    fileprivate func checkInput() -> Bool {
        if !validMobile(mobile: self.phoneField.text!) {
            return false
        }
        if !validPassword(password: self.codeField.text!) {
            return false
        }
        
        return true
    }
}
